/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 04/11/2011, 22:16:53
 */

package br.com.bodylimit.fronteira;

import br.com.bodylimit.Main;
import br.com.bodylimit.model.Presenca;
import br.com.bodylimit.service.AlunoService;
import br.com.bodylimit.service.PresencaService;
import br.com.bodylimit.util.Mensagem;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jõao Paulo
 */
public class MainFrame extends javax.swing.JFrame {

    List<Presenca> presencas = null;

    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();
        atualizarTabelaPresenca();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        pnlPresenca = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblPresenca = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        pnlPagamento = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnuFerramentas = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sistema de Gerencimanto Body Limit");
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
        });

        tabbedPane.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        tblPresenca.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Matricula", "Nome", "Hora"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblPresenca.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tblPresenca);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/bodylimit/images/BtnMais.png"))); // NOI18N
        jButton1.setText("Dar Presenca");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/bodylimit/images/BtnX.png"))); // NOI18N
        jButton2.setText("Remover Presenca");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel2.setText("Alunos presentes hoje: ");

        javax.swing.GroupLayout pnlPresencaLayout = new javax.swing.GroupLayout(pnlPresenca);
        pnlPresenca.setLayout(pnlPresencaLayout);
        pnlPresencaLayout.setHorizontalGroup(
            pnlPresencaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPresencaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPresencaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlPresencaLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(pnlPresencaLayout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18))
                    .addGroup(pnlPresencaLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addContainerGap(449, Short.MAX_VALUE))))
        );
        pnlPresencaLayout.setVerticalGroup(
            pnlPresencaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPresencaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 372, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlPresencaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabbedPane.addTab("Presença", pnlPresenca);

        javax.swing.GroupLayout pnlPagamentoLayout = new javax.swing.GroupLayout(pnlPagamento);
        pnlPagamento.setLayout(pnlPagamentoLayout);
        pnlPagamentoLayout.setHorizontalGroup(
            pnlPagamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 597, Short.MAX_VALUE)
        );
        pnlPagamentoLayout.setVerticalGroup(
            pnlPagamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 475, Short.MAX_VALUE)
        );

        tabbedPane.addTab("Pagamentos", pnlPagamento);

        jLabel1.setFont(new java.awt.Font("Stencil", 0, 48));
        jLabel1.setText("Body Limit Academia");

        mnuFerramentas.setText("Ferramentas");

        jMenuItem1.setText("Alunos");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        mnuFerramentas.add(jMenuItem1);

        jMenuItem2.setText("Exercicios");
        mnuFerramentas.add(jMenuItem2);

        jMenuItem3.setText("Funcionarios");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        mnuFerramentas.add(jMenuItem3);

        jMenuItem4.setText("Pagamentos");
        mnuFerramentas.add(jMenuItem4);

        jMenuBar1.add(mnuFerramentas);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 543, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 602, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    public void atualizarTabelaPresenca() {
        

        presencas = new PresencaService().listar();

        if(presencas != null) {

            DefaultTableModel model = (DefaultTableModel) tblPresenca.getModel();
            model.setRowCount(0);
            for(Presenca p : presencas) {
                String hora = new SimpleDateFormat("hh:mm").format(p.getData());
                model.addRow(new Object[]{p.getAluno().getMatricula(), p.getAluno().getNome(), hora});
            }
        }
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int option;
        Presenca p;
        int linha = tblPresenca.getSelectedRow();

        if(linha == -1) {
            Mensagem.msgAviso("Selecione uma presenca antes de exluir.");
            return;
        }

        if(presencas == null || presencas.isEmpty()) {
            Mensagem.msgInfo("Ainda não existe aluno presente.");
            return;
        }

        p = presencas.get(linha);
        
        option = JOptionPane.showConfirmDialog(this, "Tem certeza que deseja excluir a presenca de " + p.getAluno().getNome() + "?", "Atenção!", JOptionPane.YES_NO_OPTION);

        if(option == JOptionPane.YES_OPTION) {
            new PresencaService().remover(p);
            atualizarTabelaPresenca();
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    /* MENU FERRAMENTAS */
    /* Alunos */
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        Main.getAlunoFrame().setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    /* Funcionários */
    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        Main.getFuncionarioFrame().setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
        // TODO add your handling code here:
        atualizarTabelaPresenca();
    }//GEN-LAST:event_formFocusGained

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        new CrudFrameFactory().getPresencaFrame();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenu mnuFerramentas;
    private javax.swing.JPanel pnlPagamento;
    private javax.swing.JPanel pnlPresenca;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JTable tblPresenca;
    // End of variables declaration//GEN-END:variables

}
